//
//  UIView+Frame.m
//  weibo
//
//  Created by apple on 17/1/3.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "UIView+Extension.h"

@implementation UIView (Extension)
-(void)setX:(CGFloat)x{
    CGRect frame = self.frame;
    frame.origin.x = x;
    self.frame = frame;
}
-(CGFloat)x{
    return self.frame.origin.x;
}
-(void)setY:(CGFloat)y{
    CGRect frame = self.frame;
    frame.origin.y = y;
    self.frame = frame;
}
-(CGFloat)y{
    return self.frame.origin.y;
}
-(void)setOrigin:(CGPoint)origin{
    CGRect frame = self.frame;
    frame.origin = origin;
    self.frame = frame;
}
-(CGPoint)origin{
    return self.frame.origin;
}


-(void)setMaxX:(CGFloat)maxX{
    CGRect frame = self.frame;
    frame.origin.x = CGRectGetMaxX(self.frame) - maxX;
    self.frame = frame;
}
-(CGFloat)maxX{
    
    return CGRectGetMaxX(self.frame);
}


-(void)setMaxY:(CGFloat)maxY{
    CGRect frame = self.frame;
    frame.origin.x = CGRectGetMaxY(self.frame) - maxY;
    self.frame = frame;
}
-(CGFloat)maxY{
    return CGRectGetMaxY(self.frame);
}

-(void)setWidth:(CGFloat)width{
    CGRect frame = self.frame;
    frame.size.width = width;
    self.frame = frame;
    
}
-(CGFloat)width{
    return self.frame.size.width;
}
-(void)setHeight:(CGFloat)height{
    CGRect frame = self.frame;
    frame.size.height = height;
    self.frame = frame;
}
-(CGFloat)height{
    return self.frame.size.height;
}
-(void)setSize:(CGSize)size{
    CGRect frame = self.frame;
    frame.size = size;
    self.frame = frame;
}
-(CGSize)size{
    return self.frame.size;
}
- (void)setCenterX:(CGFloat)centerX{
    CGPoint center = self.center;
    center.x = centerX;
    self.center = center;
}

- (CGFloat)centerX{
    return self.center.x;
}

- (void)setCenterY:(CGFloat)centerY{
    CGPoint center = self.center;
    center.y = centerY;
    self.center = center;
}

- (CGFloat)centerY{
    return self.center.y;
}

-(void)setLayerWithRadius:(CGFloat)readius color:(UIColor *)color width:(CGFloat )width{
    
    self.layer.masksToBounds = YES;
    self.layer.borderWidth = width;
    self.layer.borderColor = color.CGColor;
    self.layer.cornerRadius = readius;
}

-(void)setLayerRadius:(CGFloat)readius borderColor:(UIColor *)color borderWidth:(CGFloat )width shadowColor:(UIColor *)scolor shadowOffset:(CGSize)size shadowOpacity:(CGFloat)opacity shadowRadius:(CGFloat)shadowRadius{
    self.layer.cornerRadius = readius;
    self.layer.borderColor  = color.CGColor;
    self.layer.borderWidth  = width;
    self.layer.shadowColor  = scolor.CGColor;
    self.layer.shadowOffset = size;
    self.layer.shadowOpacity= opacity;
    self.layer.shadowRadius = shadowRadius;
    self.layer.masksToBounds = YES;
}
-(void)shakeAnimation{
    CABasicAnimation* shake = [CABasicAnimation animationWithKeyPath:@"transform.translation.x"];
    shake.fromValue = [NSNumber numberWithFloat:-3];
    shake.toValue = [NSNumber numberWithFloat:3];
    shake.duration = 0.1;//执行时间
    shake.autoreverses = YES; //是否重复
    shake.repeatCount = 2;//次数
    [self.layer addAnimation:shake forKey:@"shakeAnimation"];
}




-(UIImage *)captureImage{
    CGRect screenRect = [self bounds];
    UIGraphicsBeginImageContext(screenRect.size);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    [self.layer renderInContext:ctx];
    UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

-(void)transfromViewsx:(CGFloat)sx sy:(CGFloat)sy{
    
    [UIView animateWithDuration:0.2 animations:^{
        self.transform = CGAffineTransformMakeScale(sx,sy);
    } completion:^(BOOL finished) {
        self.transform = CGAffineTransformIdentity;
    }];
}

- (UIViewController*)getViewController{
    for (UIView* next = [self superview]; next; next = next.superview){
        UIResponder* nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]){
            return (UIViewController*)nextResponder;
        }
    }
    return nil;
}




@end
